﻿using System;
using System.Linq;
using System.Text.RegularExpressions;
using Biz.Buffer;
using Biz.EF.Gateway.Monitor;
using Biz.EF.Platform;
using Hjt212Parser;
using log4net;

namespace Biz.Process
{
    public class DownlineCmdInstructionMonitor
    {
        private static readonly ILog Log = LogManager.GetLogger(typeof(DownlineCmdInstructionMonitor));

        //public static void Gen3194CmdToGateway(GatewayMonitorContext gCtx, PlatformContext pCtx)
        //{
        //    try
        //    {
        //        var enterprises = pCtx.报警设置.Where(t => t.发送结果标志ID == 201).Select(t => t.EnterpriseInfor).Distinct();
        //        foreach (var enterprise in enterprises)
        //        {
        //            var cmds = pCtx.报警设置.Where(t => t.企业基本信息ID == enterprise.ID).Take(1000).ToList();
        //            var cp = "";
        //            var qn = DateTime.Now.ToString("yyyyMMddHHmmssfff");
        //            foreach (var downcmd in cmds)
        //            {
        //                downcmd.发送结果标志ID = 203;
        //                downcmd.QN = qn;
        //                var factor = CommonInformation.GetFactorById(downcmd.污染物编码ID);
        //                if (factor == null) continue;

        //                if (downcmd.上限 != null)
        //                {
        //                    if (!string.IsNullOrEmpty(cp))
        //                    {
        //                        cp += ";";
        //                    }
        //                    cp += factor.污染物代码.Trim() + "-UpLimit=" + downcmd.上限;
        //                    if (downcmd.下限 != null)
        //                    {
        //                        cp += "," + factor.污染物代码.Trim() + "-DownLimit=" + downcmd.下限;
        //                    }
        //                }
        //            }
        //            foreach (var port in enterprise.EquipmentInfors)
        //            {
        //                if (!string.IsNullOrEmpty(port.企业端数采MN))
        //                {
        //                    var resultStr = "QN=" + qn + ";ST=32;" + "CN=3194" + ";PW=123456" + ";MN=" +
        //                                    port.企业端数采MN
        //                                    + ";Flag=5;CP=&&" + cp + "&&";
        //                    resultStr = resultStr.Replace(";&&", "&&");
        //                    var result = Crc.WrapByCrc(resultStr, "monitor");
        //                    var now = DateTime.Now;
        //                    send_ctr_cmd cmd = new send_ctr_cmd
        //                    {
        //                        cn = "3194",
        //                        mn = port.企业端数采MN,
        //                        qn = qn,
        //                        is_sent = false,
        //                        send_string = result,
        //                        sent_time = now,
        //                        send_time = now
        //                    };
        //                    gCtx.send_ctr_cmd.Add(cmd);
        //                }

        //                if (!string.IsNullOrEmpty(port.企业端数采二MN))
        //                {
        //                    var resultStr = "QN=" + qn + ";ST=32;" + "CN=3194" + ";PW=123456" + ";MN=" +
        //                                    port.企业端数采二MN
        //                                    + ";Flag=5;CP=&&" + cp + "&&";
        //                    resultStr = resultStr.Replace(";&&", "&&");
        //                    var result = Crc.WrapByCrc(resultStr, "monitor");
        //                    var now = DateTime.Now;
        //                    send_ctr_cmd cmd = new send_ctr_cmd
        //                    {
        //                        cn = "3194",
        //                        mn = port.企业端数采二MN,
        //                        qn = qn,
        //                        is_sent = false,
        //                        send_string = result,
        //                        sent_time = now,
        //                        send_time = now
        //                    };
        //                    gCtx.send_ctr_cmd.Add(cmd);
        //                }
        //            }

        //            gCtx.SaveChanges();
        //        }
        //        pCtx.SaveChanges();
        //    }
        //    catch (Exception ex)
        //    {
        //        Log.Error(ex);
        //    }

        //}

        public static void Gen3194CmdToGateway(GatewayMonitorContext gCtx, PlatformContext pCtx)
        {
            try
            {
                var enterprises = pCtx.报警设置.Where(t => t.发送结果标志ID == 201).Select(t => t.EnterpriseInfor).Distinct();
                foreach (var enterprise in enterprises)
                {
                    var cmds = pCtx.报警设置.Where(t => t.企业基本信息ID == enterprise.ID).Where(t => t.发送结果标志ID == 201).Take(1000).ToList();
                    var cp = "";
                    var qn = DateTime.Now.ToString("yyyyMMddHHmmssfff");
                    foreach (var downcmd in cmds)
                    {
                        downcmd.发送结果标志ID = 203;
                        downcmd.QN = qn;
                        var factor = CommonInformation.GetFactorById(downcmd.污染物编码ID);
                        if (factor == null) continue;

                        if (downcmd.上限 != null)
                        {
                            if (!string.IsNullOrEmpty(cp))
                            {
                                cp += ";";
                            }
                            cp += factor.污染物代码.Trim() + "-UpLimit=" + downcmd.上限;
                            if (downcmd.下限 != null)
                            {
                                cp += "," + factor.污染物代码.Trim() + "-DownLimit=" + downcmd.下限;
                            }
                        }
                    }
                    foreach (var port in enterprise.EquipmentInfors)
                    {
                        if (!string.IsNullOrEmpty(port.企业端数采MN))
                        {
                            var resultStr = "QN=" + qn + ";ST=32;" + "CN=3194" + ";PW=123456" + ";MN=" +
                                            port.企业端数采MN
                                            + ";Flag=5;CP=&&" + cp + "&&";
                            resultStr = resultStr.Replace(";&&", "&&");
                            var result = Crc.WrapByCrc(resultStr, "monitor");
                            var now = DateTime.Now;
                            send_ctr_cmd cmd = new send_ctr_cmd
                            {
                                cn = "3194",
                                mn = port.企业端数采MN,
                                qn = qn,
                                is_sent = false,
                                send_string = result,
                                sent_time = now,
                                send_time = now
                            };
                            gCtx.send_ctr_cmd.Add(cmd);
                        }

                        if (!string.IsNullOrEmpty(port.企业端数采二MN))
                        {
                            var resultStr = "QN=" + qn + ";ST=32;" + "CN=3194" + ";PW=123456" + ";MN=" +
                                            port.企业端数采二MN
                                            + ";Flag=5;CP=&&" + cp + "&&";
                            resultStr = resultStr.Replace(";&&", "&&");
                            var result = Crc.WrapByCrc(resultStr, "monitor");
                            var now = DateTime.Now;
                            send_ctr_cmd cmd = new send_ctr_cmd
                            {
                                cn = "3194",
                                mn = port.企业端数采二MN,
                                qn = qn,
                                is_sent = false,
                                send_string = result,
                                sent_time = now,
                                send_time = now
                            };
                            gCtx.send_ctr_cmd.Add(cmd);
                        }
                    }

                    gCtx.SaveChanges();
                }
                pCtx.SaveChanges();
            }
            catch (Exception ex)
            {
                Log.Error(ex);
            }

        }

        public static void Gen3012CmdToGateway(GatewayMonitorContext gCtx, PlatformContext pCtx)
        {
            try
            {
                var cmds = pCtx.反控采样.Where(t => t.执行结果标志ID == 601).Take(1000).ToList();
                foreach (var downcmd in cmds)
                {
                    downcmd.执行结果标志ID = 602;
                    var port = CommonInformation.GetLetportById(downcmd.设备信息ID);
                    if (string.IsNullOrEmpty(port?.企业端数采MN)) continue;
                    if (downcmd.QN == null)
                    {
                        downcmd.QN = DateTime.Now.ToString("yyyyMMddHHmmssfff");
                    }

                    var resultStr = "QN=" + downcmd.QN + ";ST=32;" + "CN=3012" + ";PW=123456" + ";MN=" + port.企业端数采MN
                                        + ";Flag=5;CP=&&&&";
                    var result = Crc.WrapByCrc(resultStr, "monitor");
                    var now = DateTime.Now;
                    send_ctr_cmd cmd = new send_ctr_cmd
                    {
                        cn = "3012",
                        mn = port.企业端数采MN,
                        qn = downcmd.QN,
                        is_sent = false,
                        send_string = result,
                        sent_time = now,
                        send_time = now
                    };
                    gCtx.send_ctr_cmd.Add(cmd);
                    gCtx.SaveChanges();
                }
                pCtx.SaveChanges();
            }
            catch (Exception ex)
            {
                Log.Error(ex);
            }

        }

        public static void Gen3015CmdToGateway(GatewayMonitorContext gCtx, PlatformContext pCtx)
        {
            try
            {
                var cmds = pCtx.留样控制.Where(t => t.执行结果标志ID == 601).Take(1000).ToList();
                foreach (var downcmd in cmds)
                {
                    downcmd.执行结果标志ID = 602;
                    var port = CommonInformation.GetLetportById(downcmd.设备信息ID);
                    if (string.IsNullOrEmpty(port?.企业端数采MN)) continue;
                    if (downcmd.QN == null)
                    {
                        downcmd.QN = DateTime.Now.ToString("yyyyMMddHHmmssfff");
                    }
                    var no = Regex.Match(downcmd.瓶号, "[0-9]+").Value;
                    var resultStr = "QN=" + downcmd.QN + ";ST=32;" + "CN=3015" + ";PW=123456" + ";MN=" + port.企业端数采MN
                                        + ";Flag=5;CP=&&VaseNo=" + no + "&&";
                    var result = Crc.WrapByCrc(resultStr, "monitor");
                    var now = DateTime.Now;
                    send_ctr_cmd cmd = new send_ctr_cmd
                    {
                        cn = "3015",
                        mn = port.企业端数采MN,
                        qn = downcmd.QN,
                        is_sent = false,
                        send_string = result,
                        sent_time = now,
                        send_time = now
                    };
                    gCtx.send_ctr_cmd.Add(cmd);
                    gCtx.SaveChanges();
                }
                pCtx.SaveChanges();
            }
            catch (Exception ex)
            {
                Log.Error(ex);
            }

        }

        public static void Gen3016CmdToGateway(GatewayMonitorContext gCtx, PlatformContext pCtx)
        {
            try
            {
                var cmds = pCtx.设置做样间隔.Where(t => t.执行结果标志ID == 601).Take(1000).ToList();
                foreach (var downcmd in cmds)
                {
                    downcmd.执行结果标志ID = 602;
                    if (string.IsNullOrEmpty(downcmd.QN))
                    {
                        downcmd.QN = DateTime.Now.ToString("yyyyMMddHHmmssfff");
                    }
                    var port = CommonInformation.GetLetportById(downcmd.设备信息ID);
                    if (string.IsNullOrEmpty(port?.企业端数采MN)) continue;

                    var resultStr = "QN=" + downcmd.QN + ";ST=32;" + "CN=3016;PW=123456" + ";MN=" + port.企业端数采MN
                                        + ";Flag=5;CP=&&PolId=" + downcmd.MonitorFactorCode.污染物代码 + ";CstartTime=000000;CTime=" + (int)(downcmd.做样间隔 ?? 60) + "&&";
                    var result = Crc.WrapByCrc(resultStr, "monitor");
                    var now = DateTime.Now;
                    send_ctr_cmd cmd = new send_ctr_cmd
                    {
                        cn = "3016",
                        mn = port.企业端数采MN,
                        qn = downcmd.QN,
                        is_sent = false,
                        send_string = result,
                        sent_time = now,
                        send_time = now
                    };
                    gCtx.send_ctr_cmd.Add(cmd);
                    gCtx.SaveChanges();
                }
                pCtx.SaveChanges();
            }
            catch (Exception ex)
            {
                Log.Error(ex);
            }

        }

        public static void Gen1062CmdToGateway(GatewayMonitorContext gCtx, PlatformContext pCtx)
        {
            try
            {
                var cmds = pCtx.设置数据上传间隔.Where(t => t.执行结果标志ID == 601).Take(1000).ToList();
                foreach (var downcmd in cmds)
                {
                    downcmd.执行结果标志ID = 602;
                    if (string.IsNullOrEmpty(downcmd.QN))
                    {
                        downcmd.QN = DateTime.Now.ToString("yyyyMMddHHmmssfff");
                    }
                    var port = CommonInformation.GetLetportById(downcmd.设备信息ID);
                    if (string.IsNullOrEmpty(port?.企业端数采MN)) continue;

                    var resultStr = "QN=" + downcmd.QN + ";ST=32;" + "CN=1062;PW=123456" + ";MN=" + port.企业端数采MN
                                        + ";Flag=5;CP=&&RtdInterval=" + (downcmd.上传间隔 ?? 5) + "&&";
                    var result = Crc.WrapByCrc(resultStr, "monitor");
                    var now = DateTime.Now;
                    send_ctr_cmd cmd = new send_ctr_cmd
                    {
                        cn = "1062",
                        mn = port.企业端数采MN,
                        qn = downcmd.QN,
                        is_sent = false,
                        send_string = result,
                        sent_time = now,
                        send_time = now
                    };
                    gCtx.send_ctr_cmd.Add(cmd);
                    gCtx.SaveChanges();
                }
                pCtx.SaveChanges();
            }
            catch (Exception ex)
            {
                Log.Error(ex);
            }

        }

    }
}
